3
3
.
.
1
1
.
.
5
5
C
C
o
o
n
n
d
d
i
i
t
t
i
i
o
o
n
n
a
a
l
l
V
V
i
i
e
e
w
w
I
I
n
n
f
f
o
o
[
[
R
R
]
]
This tutorial shows how to show or hide a View based on some condition.
E
E
x
x
a
a
m
m
p
p
l
l
e
e
In this example we show or hide second Text View based on the value of show Boolean Variable.
MainActivity.kt
package com.example.testcompose
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.Text
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.setContent
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Column(
modifier = Modifier.fillMaxSize(),
horizontalGravity = Alignment.CenterHorizontally
) {
var show = true
Text("First")
if(show) { Text("Show or Hide") }
}
}
}
}
show = true show = false